//index.js
import wxParse from "../../wxParse/wxParse.js"
var app = getApp();

Page({
  data: {
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    indexnb: [{
        id: "1",
        image: "https://cdn2.odeman.hk/mobile2018/miniapp/1.png",
        title: "冬令营"
      },
      {
        id: "2",
        image: "https://cdn2.odeman.hk/mobile2018/miniapp/2.png",
        title: "研学旅行"
      },
      {
        id: "3",
        image: "https://cdn2.odeman.hk/mobile2018/miniapp/3.png",
        title: "国际游学"
      },
      {
        id: "4",
        image: "https://cdn2.odeman.hk/mobile2018/miniapp/1020079646104.png",
        title: "视频库"
      },
      {
        id: "5",
        image: "https://cdn2.odeman.hk/mobile2018/miniapp/5.png",
        title: "周末活动"
      },
      {
        id: "6",
        image: "https://cdn2.odeman.hk/mobile2018/miniapp/6.png",
        title: "周刊"
      },
      {
        id: "7",
        image: "https://cdn2.odeman.hk/mobile2018/miniapp/7.png",
        title: "营地"
      },
      {
        id: "8",
        image: "https://cdn2.odeman.hk/mobile2018/miniapp/8.png",
        title: "团队定制"
      }
    ],
    banner: [],
    jdlists: [],
    tslists: [],
    gjlists: [],
    zmlists: [],
    city: "重庆",
    list: "提交信息",
    areaid: "",
    userName: "",
    mobile: ""
  },
  isLogin(event){
    wxParse.isLogin(event)
  },
  call(event) {
    wxParse.call(event)
  },
  onShow: function() {
    var _this = this
    wx.getStorage({
      key: 'cityName',
      success: function(res) {
        _this.setData({
          city: res.data
        })
      }
    })

   
  },
  onLoad: function(options) {
    console.log(options)
    if (options.areaid) {
      wx.setStorageSync('areaid', options.areaid)
    }
    if (options.cityName) {
      wx.setStorageSync('cityName', options.cityName)
    }
// 如果用户已经选择了城市则跳转到首页，否则跳到城市选择页
    var value = wx.getStorageSync('cityName')
    if (value) {
      this.indexRequest()
    } else {
      wx.navigateTo({
        url: '/pages/index_city/index_city'
      })
    }
    this.setData({
      tel: wx.getStorageSync('phoneNumber')
    })
    
  },
  userName: function(e) {
    this.setData({
      userName: e.detail.value
    })
  },
  onShareAppMessage: function (res) {
    console.log(wx.getStorageSync('areaid'))
    console.log(wx.getStorageSync('cityName'))
    return {
      title: this.data.title,
      desc: "这是描述",
      path: "/pages/index_index/index_index?areaid=" + wx.getStorageSync('areaid') +
        "&cityName=" + wx.getStorageSync('cityName')
    }
  },
  userMobile: function(e) {
    this.setData({
      mobile: e.detail.value
    })
  },

  // 提交咨询
  submitUserInfo: function() {
    var _this = this
    var mobileRegex = /^1[3|4|5|6|7|8|9]\d{9}$/
    if (_this.data.userName == "") {
      wx.showModal({
        title: '',
        content: '请输入您的姓名'
      })
    } else if (!mobileRegex.test(_this.data.mobile)) {
      wx.showModal({
        title: '',
        content: '请输入正确的手机号'
      })
    } else {
      wx.showLoading({
        title: '提交中',
      })

      wx.request({
        url: 'https://miniapp.odeman.com/summercamp/extension.php',
        data: {
          xm: _this.data.userName,
          dh: _this.data.mobile
        },
        method: 'post',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success(res) {
          // 您的信息已发送，我们将很快联系您，谢谢您的支持！
          wx.hideLoading()
          if (res.data.status == "success") {
            wx.showModal({
              title: '',
              content: '您的信息已发送，我们将很快联系您，谢谢您的支持！',
              success(res) {
                _this.setData({
                  list: "已提交"
                })
                if (res.confirm) {
                  console.log('用户点击确定')
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
          console.log(res.data)
        }
      })
    }
  },
  //夏令营点击跳转
  navUrl: function(e) {
    var _this = this
    console.log(app.appData.userid)
    var ids = e.currentTarget.dataset.id;
    wx.setStorageSync('ids', ids)
    wx.navigateTo({
      url: '/pages/activity_detail/activity_detail?areaid=' + wx.getStorageSync('areaid') + '&ids=' + ids,
    })
  },
  indexRequest: function() {
    var _this = this
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: 'https://miniapp.odeman.com/summercamp/index.php?areaid=' + wx.getStorageSync('areaid'),
      header: {
        'Content-Type': 'application/json'
      },
      success: function(res) {
        wx.hideLoading()
        _this.setData({
          banner: res.data.banners,
          indexList: res.data.list
        })
      }
    })
  },
  goCampsite: function() {
    wx.navigateTo({
      url: '/pages/camp/camp',
    })
  },
  international: function() {
    wx.navigateTo({
      url: '/pages/international/international',
    })
  },
  runSmallBoy: function() {
    wx.setStorageSync('turnType', 0)
    wx.navigateTo({
      url: '/pages/activity_list/activity_list'
    })
  },
  navTap: function(e) {
    var id = e.currentTarget.dataset.id;
    console.log(id)
    var areaid = wx.getStorageSync('areaid')
    if (id == 7) {
      wx.navigateTo({
        url: '/pages/camp/camp?areaid=' + areaid,
      })
    } else if (id == 6) {
      wx.navigateTo({
        url: '/pages/weekly/weekly?areaid=' + areaid,
      })
    } else if (id == 8) {
      wx.navigateTo({
        url: '/pages/customized/customized?areaid=' + areaid,
      })
    } else if (id == 1) {
      // console.log("1")
      wx.setStorageSync('turnType', 0)
      wx.navigateTo({
        url: '/pages/activity_list/activity_list'
      })
    } else if (id == 2) {
      wx.setStorageSync('turnType', 0)
      wx.navigateTo({
        url: '/pages/activity_list/activity_list'
      })
    } else if (id == 3) {
      wx.setStorageSync('turnType', 1)
      wx.navigateTo({
        url: '/pages/activity_list/activity_list'
      })
    } else if (id == 5) {
      wx.setStorageSync('turnType', 2)
      wx.navigateTo({
        url: '/pages/activity_list/activity_list'
      })
    } else if (id == 4) {
      wx.navigateTo({
        url: '/pages/video/video'
      })
    }
  },
  about: function() {
    app.about()
  },
  contact: function() {
    app.contact()
  },
  footerNav: function(e) {
    var areaid = app.appData.userinfo.areaid;
    var userid = app.appData.userinfo.userid;
    var id = e.currentTarget.dataset.id;
    if (id == 1) {
      wx.navigateTo({
        url: '/pages/index_index/index_index?areaid=' + areaid,
      })
    } else if (id == 2) {
      wx.navigateTo({
        url: '/pages/activity_list/activity_list?areaid=' + areaid,
      })
    } else {
      wx.navigateTo({
        url: '/pages/user/user?areaid=' + areaid + '&userid=' + userid,
      })
    }

  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})